/*
 * VueImageMiniature.java
 *
 * Created on 21 novembre 2007, 15:09
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package Vue;

import Modele.*;

import java.awt.Cursor;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.Transferable;
import java.awt.datatransfer.UnsupportedFlavorException;
import java.awt.dnd.DnDConstants;
import java.awt.dnd.DragGestureEvent;
import java.awt.dnd.DragGestureListener;
import java.awt.dnd.DragGestureRecognizer;
import java.awt.dnd.DragSource;
import java.awt.dnd.DragSourceDragEvent;
import java.awt.dnd.DragSourceDropEvent;
import java.awt.dnd.DragSourceEvent;
import java.awt.dnd.DragSourceListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.io.IOException;
import javax.swing.JOptionPane;
import javax.swing.JPanel;


/**
 *
 * @author Soufiane GARCH
 * If you want to change anything in this source, please contact me
 */
public class VueImageMiniature extends JPanel implements Transferable{
    private static final int MAX = 80;
    final public static DataFlavor FLAVOR = new DataFlavor(VueImageMiniature.class, "Image");
    private static DataFlavor FLAVORS[] = { FLAVOR };
    
    
    private ModeleImage mImg;
    private VuePrincipale vuePrincipale;
    
    /** Creates a new instance of VueImageMiniature */
    public VueImageMiniature(ModeleImage i, VuePrincipale vP) {
        super();
        mImg = i;
        this.vuePrincipale = vP;
        /*
        this.setLayout(new BoxLayout(this,BoxLayout.PAGE_AXIS));
        JLabel label = new JLabel(mImg.getNom());
        JPanel lab = new JPanel();
	lab.add(label);
	this.add(lab);
	*/
	this.setPreferredSize(new Dimension(80, 80));
        this.addMouseListener(new MouseListener() 
		{
			public void mouseClicked(MouseEvent e)
			{
                            if (e.getClickCount() == 2) {
                                vuePrincipale.afficheImage(mImg);
                                return;
                            }
			}
			public void mouseEntered(MouseEvent e) {}
			public void mouseExited(MouseEvent e) {}
			public void mousePressed(MouseEvent e) {}
			public void mouseReleased(MouseEvent e) {}
		});
		
	//this.add(this);
        DragSource dragSource = DragSource.getDefaultDragSource() ;
        DragGestureRecognizer dgr = dragSource.createDefaultDragGestureRecognizer(this, DnDConstants.ACTION_COPY_OR_MOVE, new DragAndDropSource(this,dragSource) );
    }
    
    @Override
    public void paintComponent(Graphics g) {
        if (mImg != null){
            int largeur = mImg.getLargeur();
            int hauteur = mImg.getHauteur();
            float coef = mImg.getLargeur() / mImg.getHauteur();
            
            if ( (MAX / coef) > MAX ){
                largeur = (int) (MAX * coef);
                hauteur = MAX;
            }else{
                largeur = MAX;
                hauteur = (int) (MAX / coef);
            }
            
            super.paintComponent(g);

            g.drawImage(mImg.getImage(),0,0,largeur, hauteur, this);
        }
    } 
    /*
    @Override
    public void repaint(){
        super.repaint();
    }*/
    
    public void update(){
        super.repaint();
    }
    
    public String getNom()
    {
        return mImg.getAdresse();
    }

    public DataFlavor[] getTransferDataFlavors() {
        return FLAVORS;
    }

    public boolean isDataFlavorSupported(DataFlavor flavor) {
        return flavor.equals(FLAVOR);
    }

    public Object getTransferData(DataFlavor flavor) throws UnsupportedFlavorException, IOException {
        if (flavor.equals(FLAVOR)) {
            return this.mImg.getFile().getAbsolutePath();
        } else throw new UnsupportedFlavorException(flavor);
    }
    
    class DragAndDropSource implements DragGestureListener,DragSourceListener {

        private VueImageMiniature vIM;
        private DragSource dragSource;
    
        public DragAndDropSource(VueImageMiniature vIM, DragSource dragSource){
        
            this.vIM = vIM;
            this.dragSource = dragSource;
        }
        
        public void dragGestureRecognized(DragGestureEvent dge) {
               
            //Get the Transferable Object
            Transferable transferable = (VueImageMiniature) vIM;
     
            if(!(transferable instanceof VueImageMiniature)) return;
     
            Cursor cursor = DragSource.DefaultMoveDrop;
        
            dragSource.startDrag(dge, cursor, transferable, this);

        }


        public void dragEnter(DragSourceDragEvent dsde) {}
        
        public void dragOver(DragSourceDragEvent dsde) { }
        
        public void dropActionChanged(DragSourceDragEvent dsde) { }
        
        public void dragExit(DragSourceEvent dse) {}
        
        @SuppressWarnings("static-access")
        public void dragDropEnd(DragSourceDropEvent dsde) {
        
            if(dsde.getDropSuccess()){
                System.out.println("Drop OK");
                JOptionPane d = new JOptionPane();
                d.showMessageDialog(vuePrincipale,"L'image a été ajoutée avec succé.","Pikasial",JOptionPane.WARNING_MESSAGE);
            }else{
                JOptionPane d = new JOptionPane();
                d.showMessageDialog(vuePrincipale,"Opération non permise.","Pikasial",JOptionPane.WARNING_MESSAGE);
                System.out.println("Drop pas OK");
            }

        }
    }
    
    /*
    public static void main(String args[]) {
        ModeleImage mi1 = new ModeleImage("D:\\Mes Documents\\test\\alittlemotivation.jpg");
        ModeleImage mi2 = new ModeleImage("D:\\Mes Documents\\test\\amethyst_b.jpg");
        ModeleImage mi3 = new ModeleImage("D:\\Mes Documents\\test\\aprettygoodwall.jpg");
        ModeleImage mi4 = new ModeleImage("D:\\Mes Documents\\test\\aquacool Wide.jpg");
        
        VueImageMiniature img1 = new VueImageMiniature(mi1);
        VueImageMiniature img2 = new VueImageMiniature(mi2);
        VueImageMiniature img3 = new VueImageMiniature(mi3);
        VueImageMiniature img4 = new VueImageMiniature(mi4);
        
        JFrame fe = new JFrame();
        Container cont = fe.getContentPane();
        JPanel pan = new JPanel();
        pan.setLayout(new FlowLayout());
        pan.add(img1);
        pan.add(img2);
        pan.add(img3);
        pan.add(img4);
        cont.add(pan);
        fe.setSize(800, 600);
        fe.setEnabled(true);
        fe.setVisible(true);
    }*/
}
